<!-- Modal -->
<div class="modal fade" id="ajax_upload_file" tabindex="-1" role="dialog" aria-labelledby="ajax_upload_file">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
        <form target="hidden_frame" action="<?php echo Common::RewriteUrl("AjaxUploadFile","upload");?>" enctype="multipart/form-data"  method="post">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">文件上传</h4>
      </div>
      <div class="modal-body">
          
          
 <div class="media">
  <div class="media-left">
      <img class="media-object" id="txyl" src="/images/thumb/100x100/nopic.png" alt="" />
  </div>
  <div class="media-body verticaltop">
    
 <input type="file" id="uploadtx" name="image" class="pull-left" />
 <a class="btn btn-danger btn-xs pull-right" id="deleteimage" href="javascript:void(0);"><span class="glyphicon glyphicon-trash"></span></a>
  
  </div>
</div>
          
            
             
              
            
          
           
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="submit" class="btn btn-primary">上传</button>
      </div>
              </form>
        <iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe>
    </div>
  </div>
</div>
<script>
    var upload_parent=null;
    function setImagePreview(upload,preview) {
    
       
        var docObj=document.getElementById(upload);
        var imgObjPreview=document.getElementById(preview);
        if(docObj.files &&    docObj.files[0]){
            //火狐下，直接设img属性
            imgObjPreview.style.display = 'block';
            imgObjPreview.style.width = '100px';
            imgObjPreview.style.height = '100px';
            try{
                imgObjPreview.src = docObj.files[0].getAsDataURL();
            }catch(e){
                //火狐7以上版本不能用上面的getAsDataURL()方式获取，需要一下方式
                imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);
            }
 
 
        }else{
            //IE下，使用滤镜
            docObj.select();
            var imgSrc = document.selection.createRange().text;
            var localImagId = document.getElementById("localImag");
            //必须设置初始大小
            localImagId.style.width = "160px";
            localImagId.style.height = "120px";
            //图片异常的捕捉，防止用户修改后缀来伪造图片
            try{
                localImagId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
                localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
            }catch(e){
                alert("您上传的图片格式不正确，请重新选择!");
                return false;
            }
            imgObjPreview.style.display = 'none';
            document.selection.empty();
        }
        return true;
    
 
}
function ajax_upload_callback(data){
    if(data.status==1){
        upload_parent.attr("src",data.path);
        upload_parent.data("psrc",data.spath);
        upload_parent.change();
    }else{
        alert(data.msg);
    }
    $('#ajax_upload_file').modal('hide');
}
$('#ajax_upload_file').on('show.bs.modal', function (event) {
   upload_parent= $(event.relatedTarget);
   $("#txyl").attr("src",upload_parent.attr("src"));
   $("#txyl").data("psrc",upload_parent.data("psrc"));
   $("#deleteimage").data("psrc",upload_parent.data("psrc"));
});

$(document).ready(function(){
    $("#uploadtx").change(function(){
        setImagePreview("uploadtx","txyl");
    });
    $("#deleteimage").click(function(){
        var src=$(this).data("psrc");
        var url="<{echo Common::RewriteUrl("AjaxUploadFile","public_ajax_deleteimage");}>";
        $.post(url,{"path":src},function(){
           $("#txyl").attr("src",upload_parent.attr("src")+"?"+Math.random());
        });
    });
});
</script>